package spring.mvc.practice.controllers;

import java.text.DateFormat;

import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.beans.factory.annotation.Autowired;


import spring.mvc.practice.*;



/*
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
*/

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
	private AuthenticationTrustResolver authenticationTrustResolver;
	
	
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home() {
		
		
	if (!authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {
		//logger.info("Welcome "+ ((Registration)principal).getFirst());
		return "redirect:/home";
	}

		/* test data source set up
		try {
		Context initContext = new InitialContext();
		Context envContext  = (Context)initContext.lookup("java:/comp/env");
		DataSource ds = (DataSource)envContext.lookup("jdbc/normqa");
		Connection conn = ds.getConnection();
		PreparedStatement ps = conn.prepareStatement("select * from registrations");
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			logger.info(rs.getString("email"));
		}
		} catch (SQLException ex){
            logger.info("sql error");
        } catch (NamingException ne){
			ne.printStackTrace();
		}
		*/
		
		logger.info("login");		
			
		return "tiles.login";
		
	}


	
	
	@RequestMapping(value = "/{menuItem}", method = RequestMethod.GET)
	public String menu(Locale locale, Model model,@PathVariable String menuItem) {
		logger.info(menuItem);
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
					
		return "tiles."+ menuItem;
	}
	
	@RequestMapping(value = "/{menuItem1}/{menuItem2}", method = RequestMethod.GET)
	public String menuSub(Locale locale, Model model,@PathVariable String menuItem1,@PathVariable String menuItem2) {
		logger.info(menuItem1 + "/" + menuItem2);			
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "tiles."+ menuItem1 + "." + menuItem2;
	}


	
	
}
